#!/bin/bash

# 项目目录
PROJECT_DIR=$(dirname "$0")
BUILD_DIR="$PROJECT_DIR/build"
PYTHON_SCRIPT="$PROJECT_DIR/scripts/plot_results.py"

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# 1️⃣  清理旧的构建文件
echo -e "${GREEN}🧹 Cleaning old build files...${NC}"
rm -rf "$BUILD_DIR"

# 2️⃣  创建构建目录
echo -e "${GREEN}📁 Creating build directory...${NC}"
mkdir -p "$BUILD_DIR"

# 3️⃣  运行 CMake 配置
echo -e "${GREEN}🔧 Running CMake configuration...${NC}"
cmake -B "$BUILD_DIR"
if [ $? -ne 0 ]; then
    echo -e "${RED}❌ CMake configuration failed!${NC}"
    exit 1
fi

# 4️⃣  编译项目
echo -e "${GREEN}🛠  Building project...${NC}"
cmake --build "$BUILD_DIR"
if [ $? -ne 0 ]; then
    echo -e "${RED}❌ Build failed!${NC}"
    exit 1
fi

# 5️⃣  运行 Google Test 测试
echo -e "${GREEN}🧪 Running tests...${NC}"
"$BUILD_DIR/BigIntegerTest"
if [ $? -ne 0 ]; then
    echo -e "${RED}❌ Tests failed!${NC}"
    exit 1
fi

# 6️⃣  运行 Python 画图脚本
if [ -f "$PYTHON_SCRIPT" ]; then
    echo -e "${GREEN}📊 Running Python visualization...${NC}"
    python3 "$PYTHON_SCRIPT"
    if [ $? -ne 0 ]; then
        echo -e "${RED}❌ Python script execution failed!${NC}"
        exit 1
    fi
else
    echo -e "${RED}⚠️ Python script not found: $PYTHON_SCRIPT${NC}"
fi

echo -e "${GREEN}🎉 All steps completed successfully!${NC}"
